package syntax;
import java.util.HashSet;
import java.util.Iterator;

import common.TokenType;

public class SynchSet {
	HashSet<TokenType> set = new HashSet<TokenType>();

	public SynchSet(){
		set.add(TokenType.ENDFILE);
	}
	
	public void union(SynchSet addSet)	
	{
		for(Iterator<TokenType> it = addSet.set.iterator(); it.hasNext();)
		{
			this.set.add(it.next());
		}
	}
	
	public void union(NonterminalType addType)
	{
		TokenType addObj;
		FirstSet addSet = new FirstSet(addType);
		for(Iterator<TokenType> it = addSet.set.iterator(); it.hasNext();)
		{
			addObj=it.next();
			boolean EqualStatus=false;
			for(Iterator<TokenType> it2 = this.set.iterator(); it2.hasNext();)
			{
				if(addObj==it2.next())
				{
					EqualStatus = true;
				}
			}
			if(!EqualStatus)
			{
				this.set.add(addObj);
			}

		}
	}
	
	public void addTerminal(TokenType terminal)
	{
		boolean EqualStatus=false;
		for(Iterator<TokenType> it2 = this.set.iterator(); it2.hasNext();)
		{
			if(terminal==it2.next())
			{
				EqualStatus = true;
			}
		}
		if(!EqualStatus)
		{
			this.set.add(terminal);
		}
	}
	
	public void clear()
	{
		this.set.clear();
	}
	
	
	
	public Boolean contain( TokenType token ){
		return this.set.contains(token);
	}
	
	@Override
	public String toString(){
		StringBuilder builder = new StringBuilder();
		for(TokenType type: set)
			builder.append(type).append(", ");

		return builder.toString();
	}
}
